package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.dto.system.AssginMenuDto;
import com.zhentao.entity.system.SysMenu;
import com.zhentao.entity.system.SysRoleMenu;
import com.zhentao.mapper.SysMenuMapper;
import com.zhentao.service.SysMenuService;
import com.zhentao.service.SysRoleMenuService;
import com.zhentao.mapper.SysRoleMenuMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Lenovo
* @description 针对表【sys_role_menu(角色菜单)】的数据库操作Service实现
* @createDate 2024-09-02 08:36:07
*/
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu>
    implements SysRoleMenuService{
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Resource
    private SysMenuService sysMenuService;
    /**
     * 查询所有的菜单+根据角色查询对应的菜单
     * @param roleId
     * @return
     */
    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {
        List<SysMenu> sysMenus = sysMenuService.findNodes();
        // 查询当前角色的菜单数据
        List<Long> roleMenuIds = sysRoleMenuMapper.findSysRoleMenuByRoleId(roleId);
        // 将数据存储到Map中进行返回
        Map<String , Object> result = new HashMap<>() ;
        result.put("sysMenuList" , sysMenus) ;
        result.put("roleMenuIds" , roleMenuIds) ;
        return result;
    }
    /**
     * 保存分配的菜单
     * @param assginMenuDto
     * @return
     */
    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        //删除原有的菜单角色中间表数据
        sysRoleMenuMapper.delete(Wrappers.<SysRoleMenu>lambdaQuery().eq(SysRoleMenu::getRoleId,assginMenuDto.getRoleId()));
        // 获取菜单的id
        List<Map<String, Number>> menuInfo = assginMenuDto.getMenuIdList();
        if(menuInfo != null && menuInfo.size() > 0) {
            sysRoleMenuMapper.doAssign(assginMenuDto) ;
        }
    }
}




